﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace DataProcessing
{
    public class AnnotationFileParser
    {

        static public List<AnnotationData> GetFromFile(String fileName)
        {
            List<AnnotationData> ret = new List<AnnotationData>();
            

            FileStream stream = new FileStream(fileName, FileMode.Open);
            
            StreamReader reader = new StreamReader(stream);

            string line = reader.ReadLine();
           
            try
            {
                //1	2				7	8			12	13 			

                while (line != null)
                {
                    

                    try
                    {
                        char[] separator = { ' ', '\t' };
                        String[] columns = line.Split(separator);
                        AnnotationData data = new AnnotationData();

                        //3染色体编号
                        data.ChromeName = columns[2].Substring(3);
                        //4gene的方向，+表示从左向右，-表示从右向左
                        data.Dir = columns[3].Equals("+") ? AnnotationData.Direction.FORWARD : AnnotationData.Direction.REVERSE;
                        //5起始点位置	6结束点位置
                        data.Range.Start = uint.Parse(columns[4]);
                        data.Range.End = uint.Parse(columns[5]);

                        //9xson count
                        int xsonCount = int.Parse(columns[8]);
                        //10 xson起始	11 xson终止
                        String startString = columns[9];
                        String endString = columns[10];
                        String[] starts = startString.Split(',');
                        String[] ends = endString.Split(',');

                        for (int i = 0; i < xsonCount; i++)
                        {
                            ValueRange newRange = new ValueRange();
                            
                            newRange.Start = uint.Parse(starts[i]);
                            newRange.End = uint.Parse(ends[i]);
                            
                            data.XSonInfo.Add(newRange);
                        }
                        //基因名称	14
                        data.GeneName = columns[12];
                        ret.Add(data);
                    }
                    //某行异常不影响整个文件解析
                    catch (Exception exInner)
                    {
                        line = reader.ReadLine();
                        continue;
                    }

                    line = reader.ReadLine();
                    
                }
            }

            catch (Exception ex)
            {
                reader.Close();
                stream.Close();
                
            }

            
            reader.Close();
            stream.Close();

            return ret;
        }
    }
}

